package com.onLineCar.apply.exprot;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.util.PoitlIOUtils;
import com.onLineCar.apply.model.DriverExportRequest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.Base64;
import java.util.HashMap;

/**
 * Word文档合同模板工具类
 * @program: OnLineCar
 * @description:
 * @author: godGuang
 * @create: 2023-12-28 22:28
 **/
@Component
public class WordExportService {


    /**
     *
     * @param response
     * @param driverExportRequest 模板对应实体类
     * @throws Exception
     */

    public void exportWord(HttpServletResponse response, DriverExportRequest driverExportRequest) throws Exception {
        byte[] signatureBytes = Base64.getDecoder().decode(driverExportRequest.getSignatureBase64());
        InputStream inputStream = new ByteArrayInputStream(signatureBytes);
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("ContractTemplate.docx");
        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream).render(
                new HashMap<String, Object>(){{
                    put("driverName", driverExportRequest.getDriverName());
                    put("driverCard", driverExportRequest.getDriverCard());
                    put("driverPhone", driverExportRequest.getDriverPhone());
                    put("signature", Pictures.ofStream(inputStream, PictureType.PNG).size(140, 60).create());
                    put("signature",new URL("https://dsr-pic.oss-cn-shanghai.aliyuncs.com/2023/DECEMBER/1%20%289%29.png"));
                    LocalDate now = LocalDate.now();
                    int year = now.getYear();
                    int month = now.getMonthValue();
                    int day = now.getDayOfMonth();
                    put("year", year);
                    put("month", month);
                    put("day",day);
                    put("companySeal", Pictures.ofStream(ClassLoader.getSystemClassLoader().getResourceAsStream("dog.png"), PictureType.PNG).size(330, 255).create());
                }});
        response.setContentType("application/octet-stream;charset=utf-8");
        String fileName = "八维网约车互联网有限公司劳动合同";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace(); // 处理编码异常
        }
        response.setHeader("Content-disposition","attachment;filename=\""+fileName+".docx"+"\"");

        // HttpServletResponse response
        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        bos.flush();
        out.flush();
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);
    }
}
